"use strict";
cc._RFpush(module, '498e8MX3LpE1qWB3zZE86Nf', 'MyPolygonShape');
// Box2D\Scripts\Shape\MyPolygonShape.js

/*************************************************
 * Create   : 2016/1/30
 * Update   : 2016/1/30
 * Author   : Jeason1997
 * FileName : Box2D_EdgeShape.js
 * Describe : 
 *************************************************/

require('Shape');

window.MyPolygonShape = cc.Class({

    'extends': Shape,

    editor: {
        menu: 'i18n:Box2D.Shape.Box2D_PolygonShape.menu',
        executeInEditMode: false
    },

    properties: {
        shapeType: {
            'default': ShapeType.MYPOLYGON,
            type: ShapeType,
            readonly: true,
            visible: false
        },
        removePathInGame: {
            'default': false
        },
        MyPolyType: {
            'default': 0
        },
        Pchildren: {
            'default': [],
            type: cc.Node,
            visible: false
        },
        Qchildren: {
            'default': [],
            type: cc.Node,
            visible: false
        },
        vertexes1: {
            'default': [],
            type: cc.Node,
            notify: function notify() {
                this.Pchildren.length = 0;
                if (this.vertexes1.length > this.Pchildren.length) {
                    for (var i = this.Pchildren.length; i < this.vertexes1.length; ++i) {
                        var child = cc.instantiate(this.linePrefab);
                        child.name = 'P_' + i;
                        this.vertexes1[i] = child;
                        this.node.addChild(child);
                        this.Pchildren[i] = child;
                        if (i > 0) {
                            this.vertexes1[i - 1].getComponent(EdgeLine).nextPoint = child;
                            child.getComponent(EdgeLine).lastPoint = this.Pchildren[i - 1];
                        }
                    }
                } else {
                    for (var j = this.Pchildren.length - 1; j >= this.vertexes1.length; --j) {
                        if (j > 0) this.vertexes1[j - 1].getComponent(EdgeLine).nextPoint = null;
                        this.node.removeChild(this.Pchildren[j]);
                        this.Pchildren.length = this.Pchildren.length - 1;
                    }
                }
            }
        },
        vertexes2: {
            'default': [],
            type: cc.Node,
            notify: function notify() {
                this.Qchildren.length = 0;
                if (this.vertexes2.length > this.Qchildren.length) {
                    for (var i = this.Qchildren.length; i < this.vertexes2.length; ++i) {
                        var child = cc.instantiate(this.linePrefab);
                        child.name = 'Q_' + i;
                        this.vertexes2[i] = child;
                        this.node.addChild(child);
                        this.Qchildren[i] = child;
                        if (i > 0) {
                            this.vertexes2[i - 1].getComponent(EdgeLine).nextPoint = child;
                            child.getComponent(EdgeLine).lastPoint = this.Qchildren[i - 1];
                        }
                    }
                } else {
                    for (var j = this.Qchildren.length - 1; j >= this.vertexes2.length; --j) {
                        if (j > 0) this.vertexes2[j - 1].getComponent(EdgeLine).nextPoint = null;
                        this.node.removeChild(this.Qchildren[j]);
                        this.Qchildren.length = this.Qchildren.length - 1;
                    }
                }
            }
        },
        linePrefab: {
            'default': null,
            type: cc.Prefab
        }
    },

    onLoad: function onLoad() {
        if (this.removePathInGame) {
            this.node.removeAllChildren();
        }
    },

    getShape: function getShape() {
        var shapes = new Array(2);

        var vets = new Array(this.vertexes1.length);

        for (var i = 0; i < this.vertexes1.length; ++i) {
            var pos = this.vertexes1[i].position;
            var v = new b2Vec2(pos.x / PTM_RATIO, pos.y / PTM_RATIO);
            vets[i] = v;
        }

        var shape = new b2PolygonShape();
        shape.SetAsVector(vets, vets.length);
        shapes[0] = shape;

        var vets2 = new Array(this.vertexes2.length);

        for (var i = 0; i < this.vertexes2.length; ++i) {
            var pos = this.vertexes2[i].position;
            var v = new b2Vec2(pos.x / PTM_RATIO, pos.y / PTM_RATIO);
            vets2[i] = v;
        }

        var shape2 = new b2PolygonShape();
        shape2.SetAsVector(vets2, vets2.length);
        shapes[1] = shape2;

        return shapes;
    }
});

cc._RFpop();